package com.guigu.community.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.ProductCategory;
import com.guigu.community.pojo.Products;
import com.guigu.community.service.ProductService;
import com.guigu.community.vo.InventoryVo;
import com.guigu.community.vo.ProductRequestVo;
import com.guigu.community.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/query/{current}/{size}")
    @ResponseBody
    public PageInfo<Products> query(
                                    @PathVariable("current") Integer current,
                                    @PathVariable("size") Integer size,
                                    @RequestBody ProductVo vo){
        PageInfo<Products> productsPageInfo = productService.queryVo(current, size, vo);
        return productsPageInfo;
    }

    @PostMapping("/query2/{current}/{size}")
    @ResponseBody
    public PageInfo<Products> query2(
            @PathVariable("current") Integer current,
            @PathVariable("size") Integer size,
            @RequestBody ProductVo vo){
        vo.setStatus(String.valueOf(1));
        PageInfo<Products> productsPageInfo = productService.queryVo(current, size, vo);
        return productsPageInfo;
    }

    @PostMapping("/getOne/{proId}")
    @ResponseBody
    public Products getOne(@PathVariable("proId") Integer id){
        return productService.queryById(id);
    }

    @PostMapping("/getLevel")
    @ResponseBody
    public List<ProductCategory> getLevel(){
        return productService.queryByLevel();
    }

    @PostMapping("/queryProductType/${suppliersId}")
    @ResponseBody
    public List<Products> queryProductType(Integer suppliersId){
        return productService.queryProductType(suppliersId);
    }

    @PostMapping("/add")
    public ResponseEntity<?> addProduct(@RequestBody ProductRequestVo request) {
        boolean result = productService.addProduct(request);
        return result ?
                ResponseEntity.ok("商品添加成功") :
                ResponseEntity.status(500).body("商品添加失败");
    }

    // 文件上传处理
    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile photo) {
        // 获取源文件名称，包含有文件名和后缀
        String originName = photo.getOriginalFilename();
        // 截取后缀名
        String suffix = originName.substring(originName.lastIndexOf("."));

        // 生成唯一的文件名
        String name = UUID.randomUUID().toString();
        // 拼接成新的完整文件名
        String newFileName = name+suffix;

        // 存放图片的目录路径
        File destFile = new File("E://Img",newFileName);

        try {
            // 上传文件
            photo.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 写回可以通过图片服务器访问图片的url路径
        return "http://127.0.0.1:10001/Img/" + newFileName;
    }

    @PostMapping("updateProduct")
    @ResponseBody
    public String updateProduct(@RequestBody Products photo){
        // 确保 status 字段正确保存
        int status = Integer.parseInt(String.valueOf(photo.getStatus())); // 将字符串类型的 status 转换为整数
        photo.setStatus((byte) status); // 设置转换后的 status
        return String.valueOf(productService.updateById(photo));
    }

    @PostMapping("/select/{current}/{size}")
    @ResponseBody
    public PageInfo<Products> select(@PathVariable("current") Integer current,
                                     @PathVariable("size") Integer size,
                                     @RequestBody ProductVo vo){
        return productService.queryProductType(current, size, vo);
    }

    @PostMapping("queryId")
    @ResponseBody
    public List<Products> queryId(){
        return productService.queryId();
    }
}
